
import java.io.IOException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.browser.ProgressEvent;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.browser.TitleEvent;
import org.eclipse.swt.browser.TitleListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.genuitec.blinki.webkit.swt.JSAlertHandler;
import com.genuitec.blinki.webkit.swt.WebKitBrowser;

/**
 * Small example demonstrating 
 * 1. callbacks for javascript:alert (html/javascript in browser calls alert -> java code is executed)
 * 2. execution of javascript (javascript is called from java -> execution in browser)
 * 3. evaluation of javascript (javascript is used from java to retrieve a value from the document in the browser)
 * 
 * The program loads html from a file and displays it in a browser.
 * It waits for a button click and afterwards retrieves the value of a text field.
 * 
 * @author Peter
 *
 */


public class SimpleHtmlFormSubmission {
	public static void main(String [] args) {
		
		Display display = new Display();
		final Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
		final WebKitBrowser browser;
		try {
			browser = new WebKitBrowser(shell,SWT.NONE);
			
		} catch (SWTError e) {
			System.out.println("Could not instantiate Browser: " + e.getMessage());
			return;
		}
		browser.addTitleListener(new TitleListener() {
			public void changed(TitleEvent event) {
				shell.setText(event.title);
			}
		});
		
		browser.addProgressListener(new ProgressListener() {
			public void changed(ProgressEvent event) {
			}
			public void completed(ProgressEvent event) {
				/*
				// example how to execute javascript
				boolean result = browser.execute("document.title='"+newTitle+"'");
				if (!result) {
					// Script failed or is not supported on this platforms
					System.out.println("Script was not executed.");
				}
				*/
				
				/*
				// example how to evaluate a javascript function
				Object result = browser.evaluate("document.input.user.value");
				System.out.println(result);
				*/
				
			}
		});
		
		/** This handler is called when the browser executes the javascript:alert function
		 *  
		 */
		JSAlertHandler handler = new JSAlertHandler(){

			@Override
			public void alert(String arg0) {
	
				//by convention the button calls the alert with its name as message
				if(arg0.equals("submit_button")){
					//we retrieve the value of the text field
					Object result = browser.evaluate("javascript:getUsernameValue()");
					//and print it
					System.out.println(result);
				}
			}
			
		};
		browser.setJSAlertHandler(handler);

		//Loading an HTML document 
		//from a url
		//String url = "file:///C:/Users/Peter/Projects/eclipse/epic_workspace/netkit%20test/assets/test.html");
		//String url =  "http://www.google.com";
		//browser.setUrl(url);
		
		String html = "";
		try {
			//load a html file
			html = ResourceManager.readStringResource("the_html.html");
		} catch (IOException e) {
			e.printStackTrace();
			html="error loading html file: "+e.getMessage();
		}

		//setting the html string 
		browser.setText(html);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

	
	
	
	
}


